package com.rainwood.material.ui;

import android.content.Intent;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.rainwood.material.App;
import com.rainwood.material.R;
import com.rainwood.material.adapter.ModuleAdapter;
import com.rainwood.material.base.BaseActivity;
import com.rainwood.material.bean.ModuleBean;
import com.rainwood.material.uitls.ActivityStackManager;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: hhx
 * @Date: 2021/3/4
 * @Des: APP  主页
 */
public class MainActivity extends BaseActivity {


    private RecyclerView rvList;
    private long last;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        rvList = findViewById(R.id.rv_module_list);
    }

    @Override
    protected void initData() {
        int[] resDatums = {R.mipmap.check_out, R.mipmap.check_in};
        String[] names = {"领用", "归还"};
        List<ModuleBean> moduleList = new ArrayList<>();
        for (int i = 0; i < resDatums.length; i++) {
            ModuleBean bean = new ModuleBean();
            bean.setRes(resDatums[i]);
            bean.setName(names[i]);
            moduleList.add(bean);
        }

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        rvList.setLayoutManager(gridLayoutManager);
        ModuleAdapter moduleAdapter = new ModuleAdapter();
        moduleAdapter.setList(moduleList);
        rvList.setAdapter(moduleAdapter);

        moduleAdapter.setOnItemClickLister(pos -> {
            if (pos == 0) {
                turnToCheckOutPage();
            } else if (pos == 1) {
                turnToCheckInPage();
            }
        });

    }

    private void turnToCheckInPage() {
        startActivity(new Intent(this, CheckInActivity.class));
    }

    private void turnToCheckOutPage() {
        startActivity(new Intent(this, CheckOutActivity.class));
    }


    @Override
    public void onBackPressed() {
        if (last != 0 && (System.currentTimeMillis() - last < 2000)) {
            exit();
            super.onBackPressed();
            return;
        }
        last = System.currentTimeMillis();
    }

    private void exit() {
        ActivityStackManager.getInstance().exitApp(App.getInstance());

    }

    @Override
    public void onSuccess(String res, int Tag) {


    }

    @Override
    public void onFail(String res, int tag) {

    }

    @Override
    public void onError(String msg) {

    }
}